const mongoose = require('mongoose');

const messageSchema = new mongoose.Schema({
  // 消息所属的聊天会话
  sessionId: {
    type: String,
    required: true,
    index: true
  },
  // 发送者
  sender: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  // 接收者
  receiver: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  // 关联商品
  product: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Product',
    required: true
  },
  // 消息内容
  content: {
    type: String,
    required: true
  },
  // 消息类型(text/image)
  type: {
    type: String,
    enum: ['text', 'image'],
    default: 'text'
  },
  // 是否已读
  isRead: {
    type: Boolean,
    default: false
  },
  // 发送时间
  createdAt: {
    type: Date,
    default: Date.now
  }
});

const Message = mongoose.model('Message', messageSchema);
module.exports = Message; 